package com.supervision.manage.mapper.sqlprovider

import com.supervision.domain.bean.po.UserSearchPO
import org.apache.commons.lang3.StringUtils
import java.lang.StringBuilder

class UserSqlProvider {

    fun getUserList(param: HashMap<String, Any>): String {
        val po = param["po"] as UserSearchPO
        val limitSql = param["limitSql"] as String
        val sql = StringBuilder()
        if (limitSql.isEmpty()) {
            sql.append(" select count(*) ")
        } else {
            sql.append(" select u.* ")
        }

        sql.append(" from user u ")
        sql.append(" where u.weight >= 0 and u.type > 0 ")

        if (po.name.isNotEmpty()) {
            sql.append(" and u.name like concat('%', #{po.name}, '%') ")
        }
        if (po.phone.isNotEmpty()) {
            sql.append(" and u.phone like concat('%', #{po.phone}, '%') ")
        }
        if (po.type > 0) {
            sql.append(" and u.type = #{po.type} ")
        }

        sql.append(" order by u.id desc ")

        return sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY)
    }
}